﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Libs.Content;
using Libs.Utils;

public partial class Pages_News_Detail : System.Web.UI.Page
{
    protected int cateID = 0, newsID = 0, parentCateid = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!NewsUtils.CheckUrl(ref cateID, ref newsID))
        {
            Response.Redirect(Constant.HOME_PATH);
        }
        NewsCategories _Cate = new NewsCategories();
        _Cate = _Cate.Get(cateID);
        if (_Cate.FatherID != 0)
            parentCateid = _Cate.FatherID;
        else
            parentCateid = cateID;
        List<NewsCategories> lstSubCate = _Cate.GetList(parentCateid);
        if (lstSubCate.Count == 0 && parentCateid != 17 && parentCateid != 7 && parentCateid != 11 && parentCateid != 34 && parentCateid != 12 && parentCateid != 15 && parentCateid != 18)
            lstSubCate.Add(new NewsCategories() { CateID = _Cate.CateID, Name = _Cate.Name.ToUpper() });
        rptSubCate.DataSource = lstSubCate;
        rptSubCate.DataBind();

        _Cate = _Cate.Get(cateID);
        if (_Cate.CateID == 0)
        {
            Response.Redirect(Constant.HOME_PATH);
        }
        News _News = new News();
        _News = _News.Get(newsID);
        if (_News.NewsID == 0 || _News.Status != 3)
        {
            Response.Redirect(Constant.HOME_PATH);
        }

        lblTitle.Text = _News.Title;
        ltrLead.Text = _News.Lead;
        ltDatePublished.Text = AppUtils.DayOfWeekVN(_News.PublishedTime.DayOfWeek) + ", ngày " + string.Format("{0:dd/MM/yyy HH'h'mm}", _News.PublishedTime);
        Title = _News.Title + " - " + Constant.SiteTitle;
        ltrContent.Text = _News.Content;

        // Tin bài khác
        NewsWeb _NewsWeb = new NewsWeb();
        rptList.DataSource = _NewsWeb.GetList(cateID, newsID, 10);
        rptList.DataBind();

        // Tin bài liên quan
        rptNewsRelated.DataSource = _NewsWeb.GetTabletRelated(newsID);
        rptNewsRelated.DataBind();

        // Tag
        ltrTags.Text = Tags(_News.Tags);

        // Thêm thẻ meta
        HtmlMeta _des = new System.Web.UI.HtmlControls.HtmlMeta();
        _des.Name = "description";
        _des.Content = _News.Lead;
        Page.Header.Controls.Add(_des);

        HtmlMeta _descript = new System.Web.UI.HtmlControls.HtmlMeta();
        _descript.Name = "og:description";
        _descript.Content = _News.Lead;
        Page.Header.Controls.Add(_descript);

        HtmlMeta _img = new System.Web.UI.HtmlControls.HtmlMeta();
        _img.Name = "og:image";
        _img.Content = AppUtils.ImageUrl(_News.ImageUrl, "200", "200");
        Page.Header.Controls.Add(_img);
    }

    protected void btComment_Click(object sender, EventArgs e)
    {
        // Mã bảo vệ
        if (Session["Code"].ToString().ToLower() != txtCaptcha.Text.Trim().ToLower())
        {
            Message.Alert(Page, "Mã bảo vệ không chính xác");
            return;
        }

        int cateID = 0, newsID = 0;
        if (!NewsUtils.CheckUrl(ref cateID, ref newsID))
        {
            Response.Redirect(Constant.HOME_PATH);
        }

        var _Forum = new NewsForum();
        _Forum.NewsID = newsID;
        _Forum.Title = "";
        _Forum.Email = AppUtils.FilterTextbox(txtEmail.Text.Trim().ToLower());
        _Forum.FullName = AppUtils.FilterTextbox(txtFullName.Text.Trim());
        _Forum.Content = AppUtils.FilterTextbox(txtContent.Text.Trim());
        _Forum.Header = _Forum.FullName;
        _Forum.Status = 1;
        _Forum.Title = _Forum.FullName;
        _Forum.Add();

        Message.Alert(Page, "Cám ơn bạn đã gửi phản hồi! Phản hồi của bạn đang được duyệt.");
        txtContent.Text = "";
        txtEmail.Text = "";
        txtFullName.Text = "";
        txtCaptcha.Text = "";
    }

    private string Tags(string tags)
    {
        if (string.IsNullOrEmpty(tags)) return "";
        string[] list = tags.Split(',');

        string s = "";
        for (int i = 0; i < list.Length; i++)
        {
            list[i] = list[i].Trim();
            s = s + string.Format("<a href=\"{0}\">{1}</a>", Constant.HOME_PATH + "search.html?q=" + list[i], list[i]) + ", ";
        }
        return s;
    }
}